﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DistributionManage
{
    public partial class DistributionRelationByHos : BasePage
    {
        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        IBLL_HospitalProcureCatalog hpcCtrl = Factory.BLL_HospitalProcureCatalog();
        IBLL_FlowEngine ife = Factory.BLL_FlowEngine();

        DataTable _distibutionRelationTable;
        public DistributionRelationByHos()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"].ToString();
            ViewState["v_procureCatalogId"] = procureCatalogId;
            if (!IsPostBack)
            {
                Model.View.ProcureCatalog procureCatalog = ipb.GetProcureCatalogByID(procureCatalogId);
                lblCompany_SC.Text = procureCatalog.CompanyName_SC;
                lblCompany_ZB.Text = procureCatalog.CompanyName_TB;
                lblGoodsName.Text = procureCatalog.GoodsName;
                lblMaterial.Text = procureCatalog.Material;
                lblOutlookc.Text = procureCatalog.Outlookc;
                lblUnit.Text = procureCatalog.Unit;

                //获取配送关系列表 以备后用
                HZ.DrugTrade.Model.Query.DistributionRelation aDistributionRelation = new HZ.DrugTrade.Model.Query.DistributionRelation();
                //加入地区过滤条件，管理机构只能管理本地区下的采购单
                aDistributionRelation.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                aDistributionRelation.ProcureCatalogID = procureCatalogId;
                //aDistributionRelation.Hospital_IsUsing = true;
                aDistributionRelation.SortFields = "CompanyName_PS asc";

                int totalCount = 0;
                IList<HZ.DrugTrade.Model.View.DistributionRelation> distibutionRelationList = id.GetDistributionByWhere(aDistributionRelation, 1, 0, out totalCount);
                _distibutionRelationTable = HZ.Data.ModelConvert.GetDataTableFromModel(distibutionRelationList);

                //绑定医疗机构
                BindHospital();
            }
        }

        /// <summary>
        /// 绑定医疗机构
        /// </summary>
        private void BindHospital()
        {
            HZ.DrugTrade.Model.Query.HospitalProcureCatalog qHpc = new HZ.DrugTrade.Model.Query.HospitalProcureCatalog();
            qHpc.ProcureCatalogID = ViewState["v_procureCatalogId"].ToString();
            qHpc.SortFields = "HospitalID asc";

            int totalCount = 0;
            IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> hpcList  = hpcCtrl.GetHospitalProcureCatalog(qHpc,1,0,out totalCount);

            this.rptHospital.DataSource = hpcList;
            this.rptHospital.DataBind();

        }

        /// <summary>
        /// 绑定rptDistributionCom
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptHospital_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
             DataTable table;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this._distibutionRelationTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    dv.RowFilter = "HospitalName ='" + DataBinder.Eval(e.Item.DataItem, "HospitalName").ToString() + "'";
                    //dv.Sort = " OperatingRuleName";
                    Repeater rptDistributionCom = (Repeater)e.Item.FindControl("rptDistributionCom");
                    rptDistributionCom.DataSource = dv;
                    rptDistributionCom.DataBind();
                }
            }
        }

        /// <summary>
        /// 绑定配送的关系的流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptDistributionCom_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal literal = e.Item.FindControl("ltlFlow") as Literal;
                string flowID = literal.Text;
                Flow flow = ife.GetFlows(flowID);
                literal.Text = flow.FlowName;
            }
        }
    }
}
